package Ventanas.admin;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import BaseDeDatos.ConsultasUsuario;
import util.GetFrame;

public class PanelCrear extends JPanel implements ActionListener{
	private javax.swing.JButton bAtras;
	private javax.swing.JButton bCrear;
	private javax.swing.JLabel lEmail;
	private javax.swing.JLabel lId;
	private javax.swing.JLabel lNombre;
	private javax.swing.JLabel lPsw;
	private javax.swing.JLabel lTlf;
	private javax.swing.JTextField tfEmail;
	private javax.swing.JTextField tfId;
	private javax.swing.JTextField tfNombre;
	private javax.swing.JTextField tfPsw;
	private javax.swing.JTextField tfTlf;
	private JLabel fondo;
	private PanelInicialA inicial;
	
	public PanelCrear(PanelInicialA inicial){
		this.inicial = inicial;
        lId = new javax.swing.JLabel();
        lNombre = new javax.swing.JLabel();
        lPsw = new javax.swing.JLabel();
        lTlf = new javax.swing.JLabel();
        lEmail = new javax.swing.JLabel();
        tfId = new javax.swing.JTextField();
        tfNombre = new javax.swing.JTextField();
        tfPsw = new javax.swing.JTextField();
        tfTlf = new javax.swing.JTextField();
        tfEmail = new javax.swing.JTextField();
        bCrear = new javax.swing.JButton();
        bAtras = new javax.swing.JButton();
        fondo = new JLabel();

        setLayout(null);

        lId.setText("Id usuario");
        lId.setForeground(new java.awt.Color(255, 255, 255));
        add(lId);
        lId.setBounds(220, 100, 100, 30);

        lNombre.setText("Nombre");
        lNombre.setForeground(new java.awt.Color(255, 255, 255));
        add(lNombre);
        lNombre.setBounds(220, 150, 100, 30);

        lPsw.setText("Password");
        lPsw.setForeground(new java.awt.Color(255, 255, 255));
        add(lPsw);
        lPsw.setBounds(220, 210, 100, 30);

        lTlf.setText("Telefono");
        lTlf.setForeground(new java.awt.Color(255, 255, 255));
        add(lTlf);
        lTlf.setBounds(220, 270, 100, 30);

        lEmail.setText("Email");
        lEmail.setForeground(new java.awt.Color(255, 255, 255));
        add(lEmail);
        lEmail.setBounds(220, 330, 100, 30);
        add(tfId);
        tfId.setBounds(320, 100, 160, 30);
        add(tfNombre);
        tfNombre.setBounds(320, 150, 160, 30);
        add(tfPsw);
        tfPsw.setBounds(320, 210, 160, 30);
        add(tfTlf);
        tfTlf.setBounds(320, 270, 160, 30);
        add(tfEmail);
        tfEmail.setBounds(320, 330, 160, 30);

        bCrear.setText("Crear");
        add(bCrear);
        bCrear.setBounds(350, 410, 80, 30);
        bCrear.addActionListener(this);

        bAtras.setText("Atras");
        add(bAtras);
        bAtras.setBounds(90, 500, 70, 30);
        bAtras.addActionListener(this);
        
        
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		VentanaA ventana = GetFrame.getVentanaA(this);
		
		if(e.getSource().equals(bCrear))
		{
			if(tfEmail.getText().isEmpty()||tfId.getText().isEmpty()||tfNombre.getText().isEmpty()||tfPsw.getText().isEmpty()||tfTlf.getText().isEmpty())
			{
				JOptionPane.showMessageDialog(null, "Por favor, rellene todos los campos", "Error al añadir nuevo usuario ", JOptionPane.ERROR_MESSAGE);
				tfEmail.setText("");
				tfId.setText("");
				tfNombre.setText("");
				tfPsw.setText("");
				tfTlf.setText("");
			}
			else
			{
				JOptionPane.showMessageDialog(null, "Usuario: "+tfId.getText()+" agregado con exito.", "Nuevo usuario añadido! ", JOptionPane.INFORMATION_MESSAGE);
				ConsultasUsuario.insertarUsuario(tfId.getText(), tfPsw.getText(), tfNombre.getText(), Integer.parseInt(tfTlf.getText()), tfEmail.getText());
				this.setVisible(false);
				ventana.getContentPane().add(inicial);
				inicial.setVisible(true);
			}
			
			}
		if(e.getSource().equals(bAtras))
		{
			this.setVisible(false);
			ventana.getContentPane().add(inicial);
			inicial.setVisible(true);	
		}
		
	}

}
